#!/usr/bin/env python3
"""
生成Chrome扩展所需的不同尺寸图标
需要安装Pillow库: pip install Pillow
"""

import os
from PIL import Image, ImageDraw

def create_icon(size):
    """创建指定尺寸的图标"""
    # 创建透明背景
    img = Image.new('RGBA', (size, size), (0, 0, 0, 0))
    draw = ImageDraw.Draw(img)
    
    # 计算缩放比例
    scale = size / 128.0
    
    # 背景圆形
    margin = int(4 * scale)
    draw.ellipse([margin, margin, size-margin, size-margin], 
                 fill=(255, 107, 107, 255), outline=(255, 255, 255, 255), width=int(4*scale))
    
    # Cookie形状 (简化为圆角矩形)
    cookie_margin = int(20 * scale)
    cookie_size = size - 2 * cookie_margin
    draw.rounded_rectangle([cookie_margin, cookie_margin, 
                           size - cookie_margin, size - cookie_margin], 
                          radius=int(8*scale), fill=(139, 69, 19, 255))
    
    # Cookie芯片
    chip_positions = [
        (int(45*scale), int(45*scale), int(3*scale)),
        (int(65*scale), int(40*scale), int(2.5*scale)),
        (int(75*scale), int(55*scale), int(2*scale)),
        (int(55*scale), int(65*scale), int(2.5*scale)),
        (int(40*scale), int(70*scale), int(2*scale)),
        (int(70*scale), int(75*scale), int(2*scale))
    ]
    
    for x, y, r in chip_positions:
        if r > 0:
            draw.ellipse([x-r, y-r, x+r, y+r], fill=(255, 215, 0, 255))
    
    # 下载箭头
    arrow_y = int(20 * scale)
    arrow_size = int(15 * scale)
    draw.line([size//2, arrow_y, size//2, arrow_y + arrow_size], 
              fill=(255, 255, 255, 255), width=int(3*scale))
    draw.line([size//2 - int(6*scale), arrow_y + int(9*scale), 
               size//2, arrow_y + arrow_size], 
              fill=(255, 255, 255, 255), width=int(3*scale))
    draw.line([size//2 + int(6*scale), arrow_y + int(9*scale), 
               size//2, arrow_y + arrow_size], 
              fill=(255, 255, 255, 255), width=int(3*scale))
    
    # YouTube播放按钮
    play_x = int(45 * scale)
    play_y = int(80 * scale)
    play_size = int(10 * scale)
    
    # 播放按钮背景
    draw.rounded_rectangle([play_x, play_y, play_x + int(15*scale), play_y + play_size], 
                          radius=int(2*scale), fill=(255, 255, 255, 255))
    
    # 播放三角形
    triangle_points = [
        (play_x + int(2*scale), play_y + int(2*scale)),
        (play_x + int(2*scale), play_y + play_size - int(2*scale)),
        (play_x + int(8*scale), play_y + play_size//2)
    ]
    draw.polygon(triangle_points, fill=(0, 0, 0, 255))
    
    return img

def main():
    """主函数"""
    # 创建icons目录
    os.makedirs('icons', exist_ok=True)
    
    # 生成不同尺寸的图标
    sizes = [16, 32, 48, 128]
    
    for size in sizes:
        print(f"生成 {size}x{size} 图标...")
        icon = create_icon(size)
        icon.save(f'icons/icon{size}.png', 'PNG')
        print(f"已保存: icons/icon{size}.png")
    
    print("\n所有图标生成完成！")
    print("现在可以将扩展加载到Chrome中了。")

if __name__ == '__main__':
    main()
